/*
* @(#) Offer11.java 2018年03月07日
*
* Copyright 2010 NetEase.com, Inc. All rights reserved.
*/
package com.leo.m1803.d07;

/**
 *
 * @author xuexiaolei
 * @version 2018年03月07日
 */
public class Offer11 {
    public static double pow(double a, int x){
        if (Math.abs(a)<0.0000000001 && x < 1) { throw new RuntimeException("无意义"); }
        boolean isNeg = false;
        if (x<0){ isNeg = true; x = -x; }
        double res = doPow(a, x);
        if (isNeg){
            return 1.0/res;
        }else {
            return res;
        }
    }
    private static double doPow(double a, int x){
        if (x == 0) { return 1; }
        if (x == 1) { return a; }
        double res = doPow(a, x>>1);
        res *=res;
        if ((x&1)==1){ res*=a; }
        return res;
    }
}
